package cn.e3.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.e3.jedis.JedisClient;
import cn.e3.mapper.TbContentMapper;
import cn.e3.pagehelper.PageHelperResult;
import cn.e3.pojo.TbContent;
import cn.e3.pojo.TbContentExample;
import cn.e3.service.ContentService;
import cn.e3.utils.E3Result;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ContentServiceImpl implements ContentService{
	@Autowired
	private TbContentMapper tbContentMapper;
	@Autowired
	private JedisClient jedisClient;
	public PageHelperResult getContentList(int page, int row) {
		//pagehelpr引入
		PageHelper.startPage(page, row);
		//设置查询信息
		TbContentExample example=new TbContentExample();
		//查询
		List<TbContent> list = tbContentMapper.selectByExample(example);
		//使用pageinfo设置分页对象
		PageInfo<TbContent> pageInfo=new PageInfo<TbContent>(list);
		//设置pageresult
		PageHelperResult result=new PageHelperResult();
		result.setTotal(pageInfo.getTotal());
		result.setRows(list);
		return result;
	}
	//添加content
	@Override
	public E3Result addContent(TbContent content) {
		//设置更新时间
		content.setUpdated(new Date());
		//设置创建时间
		content.setCreated(new Date());
		tbContentMapper.insert(content);
		//更新后，同步缓存，将原来的redis缓存删除
		jedisClient.hdel("Content_list", content.getCategoryId()+"");
		return E3Result.ok();
	}

}
